package com.crm.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.Result;
import com.crm.dto.BaseStatisticsDTO;
import com.crm.dto.CluesStatisticsListDto;
import com.crm.dto.SalesStatisticDTO;
import com.crm.dto.Vo.ClueRankingVO;
import com.crm.dto.VulnerabilityReportDTO;
import com.crm.service.HomeService;
import com.crm.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping
@Slf4j
public class HomeController {

    @Autowired
    private HomeService homeService;
    @Autowired
    private ReportService reportService;

    @GetMapping("/index/getBaseInfo")
    public Result<BaseStatisticsDTO> getBaseInfo() {
        BaseStatisticsDTO statisticsDTO = new BaseStatisticsDTO();
        homeService.seleteMethod(statisticsDTO);
        return Result.success(statisticsDTO);
    }

    @GetMapping("/report/getVulnerabilityMap/{startDate}/{endDate}")
    public Result<VulnerabilityReportDTO> getVulnerabilityMap(
            @PathVariable String startDate,
            @PathVariable String endDate) {

        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);

        VulnerabilityReportDTO dto = reportService.getVulnerabilityMap(start, end);
        return Result.success(dto);
    }

    @GetMapping("/system/notice/list/0")
    private Result get(){
        return Result.success();
    }


    @GetMapping("/index/salesStatistic")
    public Result<List<ClueRankingVO>> getClueRanking(
            @RequestParam String beginCreateTime,
            @RequestParam String endCreateTime) {
        return null;

        /*List<SalesStatisticDTO> rawData = reportService.getSalesStatistic(beginCreateTime, endCreateTime);

        // 按转化率降序排序，并加上排名
        List<ClueRankingVO> result = new ArrayList<>();
        AtomicInteger rank = new AtomicInteger(1);

        rawData.stream()
                .sorted(Comparator.comparing(SalesStatisticDTO::getRadio).reversed())
                .forEach(dto -> {
                    ClueRankingVO vo = new ClueRankingVO(
                            rank.getAndIncrement(),
                            dto.getCreateBy(),
                            dto.getDeptName(),
                            dto.getNum(),
                            dto.getRadio()
                    );
                    result.add(vo);
                });

        return Result.success(result);*/
    }

    @GetMapping("/index/businessChangeStatistics")
    private Result<List<SalesStatisticDTO>> getBusinessChangeStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
                                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime) {
        log.info("商机统计龙虎榜,参数：{},{}", beginCreateTime, endCreateTime);
        // 将 LocalDate 转换为 LocalDateTime
        LocalDateTime beginDateTime = beginCreateTime.atStartOfDay();
        LocalDateTime endDateTime = LocalDateTimeUtil.endOfDay(endCreateTime.atStartOfDay());
        List<SalesStatisticDTO> list = homeService.getBusinessChangeCount(beginDateTime, endDateTime);
        return Result.success(list);
    }

    @GetMapping("/index")
    private Result<List<SalesStatisticDTO>> index(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime) {
/*
        log.info("线索龙虎榜,参数：{},{}", beginCreateTime, endCreateTime);
        // 将 LocalDate 转换为 LocalDateTime
        LocalDateTime beginDateTime = beginCreateTime.atStartOfDay();
        LocalDateTime endDateTime = LocalDateTimeUtil.endOfDay(endCreateTime.atStartOfDay());
        List<SalesStatisticDTO> list = homeService.indexCount(beginDateTime, endDateTime);
        return Result.success(list);
*/
        return null;

    }


}



